//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import yzGoodsPopup from "components/ui_components/yz_goodsPopup";
import yzSpecs from "components/ui_components/yz_specs"
import { Image as VanImage } from "vant";
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {
    VanImage,
    yzGoodsPopup,
    yzSpecs
  },
  data() {
  return {
    packageId:'', //套餐ID
    pageTemplate:'', //切换样式参数
    giftList:[],
    popupSpecs:false,
    goodData:{}, //规格弹窗数据
    goodinfo:{}, //商品详情数据
    showgp: false,//商品详情弹窗
    optionId:'', 
    giftId:'',
    totalPrice:''
  };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    giftData() {
      $http
        .get(
          "plugin.goods-package.frontend.package.package.combination-goods",
          {
            package_id: this.packageId
          },
          "添加中..."
        )
        .then(response => {
          if (response.result == 1) {
            let res = response.data;
            this.pageTemplate = res.page_template;
            this.totalPrice = res.package_price
            this.giftList = res.list;
            this.giftList.forEach((item,index) => {
              this.$set(item.goods, "popThumb", ""); //动态添加
              this.$set(item.goods, "goodsnum", 1); //动态添加
              this.$set(item.goods, "optionsId", ""); //动态添加
              this.$set(item.goods, "specsManage", []); //动态添加
              this.$set(item.goods, "popStock", item.goods.stock); //动态添加
              this.$set(
                item.goods,
                "popPrice",
                item.goods.price
              ); //动态添加
              if(item.goods.has_option && this.pageTemplate){ //默认选择第一个
                item.goods.optionsId = item.goods.has_many_options[0].id;
              }
            })
          } else {
            this.page = this.page - 1;
            this.isLoadMore = false;
            this.$dialog.alert({
              message: response.msg
            });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    showGoodPup(_id) {
      console.log(_id);
      if(!this.pageTemplate) return;
      if (this.activity_gid == _id) {
        this.showgp = true;
        return;
      }
      this.activity_gid = _id;
      $http
        .get("goods.goods.get-goods-page", { id: Number(_id), is_records_history: 0}, ".")
        .then(res => {
          if (res.result == 1) {
            this.goodinfo = res.data.get_goods;
            if (res.data.get_goods.content) {
              let str = this.goodinfo.content.replace(/src=\"http:/gi, 'src="https:');
              this.goodinfo.content = str;
            } else {
              this.goodinfo.content = "暂无该商品详情";
            }
            this.$nextTick(()=>{
              this.showgp = true;
            });
          } else {
            Toast(res, msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    goodsSpescOn(item) {  //打开规格弹窗
      console.log(item);
      this.popupSpecs = true;
      this.goodData = item;
      console.log(this.goodData);
      this.optionId = item.has_many_options[0].id;
    },
    close_yz_specs_popup(_optionsId, goodsCount, clicktype) {
      this.popupSpecs = false;
      if(clicktype === 1) {
        this.giftList.forEach((item,index) => {
          item.goods.has_many_options.forEach((optionItem,optionIndex) => {
            if(optionItem.id === _optionsId) {
              item.goods.popStock = optionItem.stock;
              item.goods.goodsnum = goodsCount;
              item.goods.optionsId = _optionsId;
              item.goods.price= optionItem.product_price;
              this.$set(item.goods, 'optionName',optionItem.title);
            }
          })
        })
        // });
      }
    },
    submitGoods() { //提交订单
      let json = {};
      let cartIds = [];
      let goodBol = true;
      json.package_id = this.packageId;
      json.goods_list = [];
      this.giftList.map((item,index) => {
        if (item.goods.has_option == 1 && (!item.goods.optionsId && item.goods.has_many_specs.length > 0)) {
          goodBol = false;
        }
        cartIds.push(item.goods?.id);
        let goodsJson = {};
        goodsJson.goods_id = item.goods?.id;
        goodsJson.option_id = '';
        if(item.goods?.has_option) {
          goodsJson.option_id = item.goods.optionsId;
        }
        goodsJson.total = 1;
        json.goods_list.push(goodsJson);
      })
      if(!goodBol) {
        this.$toast("请选择商品规格");
        return false;
      }
      this.$router.push(
        this.fun.getUrl("goodsorder",{
          packagJson: json,
        }, {
          tag: "giftBuy",
          goods_ids: JSON.stringify(cartIds)
        })
      );
    }
    
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
  
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.packageId = this.$route.params.id;
    this.giftData();
  }, //如果页面有keep-alive缓存功能，这个函数会触发
}